import Appearance from "../model/Appearance";
import { RankInfo, RankWindow } from "../ui/RankWindow";

export class RankModuleC extends ModuleC<RankModuleS, null>{
    private rankUI:RankWindow

    onStart(){
        let worldUI = <UIWidget>GameObject.findGameObjectById('0344061C')
        this.rankUI = UIService.create(RankWindow)
        worldUI.setTargetUIWidget(this.rankUI.uiObject as UserWidget)
        worldUI.hideByDistanceEnable = false
        
        Appearance.onOwnerPlayerRankChanged.add(this.refreshRankUI, this)
    }

    refreshRankUI(){
        let result:RankInfo[] = []
        for (const appearance of Appearance.AppearanceMap.values()) {
            result.push({name:appearance.playerName, rank:appearance.playerRank})
        }
        result.sort((a,b)=>{
            return b.rank - a.rank
        })
        this.rankUI.refresh(result)
    }
}

export class RankModuleS extends ModuleS<RankModuleC, null>{
    
}